//
//  NSString+Code.m
//  AirHospital
//
//  Created by C_HAO on 15/8/27.
//  Copyright (c) 2015年 C_HAO. All rights reserved.
//

#import "NSString+Code.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (Code)

- (NSString *)urlEncodeUTF8String
{
    if (self) {
        return [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }

    return nil;
}

- (NSString *)urlDecodeUTF8String
{
    if (self) {
        return [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }

    return nil;
}

+ (NSString *)urlDecodeUTF8String:(NSString *)stringText
{
    if (stringText == nil) {
        return nil;
    }

    NSString *urlStr = [stringText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    return urlStr;
}

+ (NSString *)urlEncodeUTF8String:(NSString *)stringText
{
    if (stringText == nil) {
        return nil;
    }
    
    NSString *urlStr = [stringText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    return urlStr;
}

- (NSString *)encodeUTF8String
{
    if (self) {
        return [NSString stringWithCString:[self UTF8String] encoding:NSUTF8StringEncoding];
    }
    
    return nil;
}

- (NSString *)MD5
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    NSNumber *num = [NSNumber numberWithUnsignedLong:strlen(cStr)];
    CC_MD5( cStr,[num intValue], result );
    return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ] lowercaseString];

}


@end
